import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from '@nestjs/common';
import { ApiForbiddenResponse, ApiTags } from '@nestjs/swagger';
import { Protocol } from 'src/common/decorators/protocol.decorator';
import { Public } from 'src/common/decorators/public.decorator';
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
import { CreatePostDto } from './dto/create-posts.dto';
import { UpdatePostDto } from './dto/update-posts.dto';
import { PostsService } from './posts.service';

@ApiTags('posts')
@Controller('posts')
export class PostsController {
  constructor(private readonly postsService: PostsService) {}
  @ApiForbiddenResponse({ description: 'forbidden' })
  @Public()
  @Get()
  async findAll(
    @Protocol() protocol: string,
    @Query() paginationQuery: PaginationQueryDto,
  ) {
    await new Promise((resolve) => setTimeout(() => resolve(null), 1000));
    return this.postsService.findAll(paginationQuery);
  }

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id) {
    await new Promise((resolve) => setTimeout(() => resolve(null), 1000));
    return this.postsService.findOne(id);
  }

  @Post()
  async create(@Body() createCoffeeDto: CreatePostDto) {
    return this.postsService.create(createCoffeeDto as any);
  }

  @Patch(':id')
  async update(
    @Param() { id }: { id: string },
    @Body() updateCoffeeDto: UpdatePostDto,
  ) {
    await new Promise((resolve) => setTimeout(() => resolve(null), 1000));
    return this.postsService.update(id, updateCoffeeDto as any);
  }

  @Delete(':id')
  async remove(@Param() { id }: { id: string }) {
    await new Promise((resolve) => setTimeout(() => resolve(null), 1000));
    return this.postsService.remove(id);
  }
}
